/*
 * test_cl.cpp
 *
 *  Created on: 2016年2月23日
 *      Author: guyadong
 */
#include <iostream>
#include <sstream>
#include <cmath>
#include "facecl_context.h"
#include "assert_macros.h"
#include "file_utilits.h"
#include "cl_utilits.h"
#include "gray_matrix_cl.h"
#include "img_tool.h"

using namespace gdface;
/*
比较两个积分图是否相等
*/
template<typename E>
void compare_integral(const matrix_cl<E, cl::Buffer> &src,const matrix_cl<E,cl::Buffer>&dst) {
	throw_if(src.width != dst.width || src.height != dst.height)
	if (src.v.size() != dst.v.size()) {
		std::cout << "matrix size mismatched..." << std::endl;
	}
	auto src_ptr = src.v.data();
	auto dst_ptr = dst.v.data();
	const auto src_row_stride = src.get_row_stride();
	const auto dst_row_stride = dst.get_row_stride();
	for (size_t y = 0; y < src.height; ++y
									,src_ptr+= src_row_stride
									,dst_ptr+= dst_row_stride
			) {
		for (size_t x = 0; x < src.width; ++x) {//
			if (src_ptr[x] != dst_ptr[x]) {
				std::cout << "mismatch integral matrix ! x:" << x << " y:" << y << std::endl;
				return;
			}
		}		
	}
	std::cout << "compare OK!"<< std::endl;
}
int main(int argc, char * argv[]){
	try{
		global_run_config.debug_show = true;
		global_run_config.kerne_message = true;
		global_facecl_context = facecl_context(DEFAULT_CL_ROOT);

		//const char *input_jpg_file = "D:/tmp/guyadong-1.jpg";
		const char *input_jpg_file = "D:/tmp/he049.jpg";
		//const char *input_jpg_file = "D:/tmp/girl001.jpg";
		auto jpeg_data=gdface::load_binary_file(input_jpg_file);
		auto gray_image=img_tools::decode_image(img_tools::GF_FMT_JPG,jpeg_data.data(),jpeg_data.size(),true);
		throw_if_msg(0==gray_image.pixels.size(),"decode_image fail")
		img_tools::display(gray_image,"original gray image");
		gray_matrix_cl matrix(gray_image);
		matrix.display(true,"source image");
		//auto z=matrix.zoom(0.95f);
		//z.download();
		//z.display();
		//auto z = matrix.zoom_w(23);
		auto z = matrix;
		auto integ_mat_cpu=z.integral_cpu<cl_float,4, integ_default>();//integ_count,integ_square,integ_default
		auto integ_mat_cl = z.integral<cl_float,4, integ_default>();
		//auto integ_mat_cpu = z.integral_cpu<cl_ushort, 4, integ_count>();
		//auto integ_mat_cl = z.to_matrix_cl().integral_count(4);
		tls::command_queue().finish();
		integ_mat_cl.download();
		compare_integral(integ_mat_cpu, integ_mat_cl);
		return 0;
	}catch(cl::Error &e){
		PRINT_CL_ERROR_COUT(e);
	}catch(face_cl_exception &e){
		cout<<e.what()<<endl;
	}catch(std::exception &e){
		cout<<"exception:"<<e.what()<<endl;
	}catch(...){
		cout<<"exception:unknow"<<endl;
	}
}
